﻿using System;
using System.Linq;
using SqlSugar;

namespace DatabaseTool.util
{
    public class ClientUtil
    {
        public static SqlSugarClient GetConnection(string ip,string username,string password,string port,string schema)
        {
            SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = "server="+ip+";port="+port+";user="+username+";password="+password+";database="+schema+";",
                DbType = DbType.MySql,
                IsAutoCloseConnection = true,
                InitKeyType = InitKeyType.Attribute
            });
            //Print sql
            db.Aop.OnLogExecuting = (sql, pars) =>
            {
                Console.WriteLine(sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
                Console.WriteLine();
            };
            return db;
        }
        
    }
}